<?php

defined('BASEPATH') or die('No direct script access allowed');

/**
 * 记账控制器
 * @author  Karson
 * @name    family.php
 * @since   2012-12-2 23:37:51
 */
class Family_Controller extends Controller {

    function _init() {
        check_level();
        $this->load->model("family");
    }

    function index() {
        $this->load->model("family");
        $familyinfo = $this->family->get_family();
        if ($this->auth->fields['family_id'] > 0 && !$familyinfo) {
            user_basic(array('family_id' => 0));
        }
        render("family/index", array('familyinfo' => $familyinfo, 'familylist' => $this->family->get_family_list()));
    }

    function member() {
        render("family/member");
    }

    function get_uuid() {
        json_output(0, get_uuid());
    }

    function add() {
        if ($this->form->is_submit()) {
            $status = 'n';
            $info = '';
            $data = array();
            $name = $this->input->post("name");
            $desc = $this->input->post("desc");
            if ($name == '') {
                $info = '家庭组名称不能为空!';
            } else if (strlen($name) > 30) {
                $info = "家庭组名称过长!";
            } else {
                $value = array(
                    'uuid' => get_uuid(), 'user_id' => $this->auth->user_id,
                    'name' => $name, 'createtime' => TIME, 'desc' => $desc
                );
                $value['id'] = $this->db->insert("{pre}family", $value);
                user_basic(array('family_id' => $value['id']));
                $status = 'y';
                $info = '添加成功！';
                $data = $value;
            }
            dialog_output($status, $info, $data);
        }
        render("family/add");
    }

    function edit($id = 0) {
        $family = $this->family->get_family($id);
        if ($family) {
            if ($this->form->is_submit()) {
                $status = 'n';
                $info = '';
                $data = array();
                $name = $this->input->post("name");
                $uuid = $this->input->post("uuid");
                $desc = $this->input->post("desc");
                if ($name == '') {
                    $info = "家庭组名称不能为空!";
                } else if (strlen($name) > 30) {
                    $info = "家庭组名称过长!";
                } else if (strlen($uuid) > 36) {
                    $info = "FID过长!";
                } else if ($uuid == '') {
                    $info = "FID不能为空!";
                } else {
                    $exist = $this->db->one("SELECT * FROM {pre}family WHERE uuid='{$uuid}' AND id!='{$id}'");
                    if (!$exist) {
                        $value = array('name' => $name, 'desc' => $desc, 'uuid' => $uuid,);
                        $value['id'] = $this->db->update("{pre}family", $value, array('id' => $id));
                        $status = 'y';
                        $info = '提交成功！';
                        $data = $value;
                    } else {
                        $info = 'FID已经存在请重新生成!';
                    }
                }
                dialog_output($status, $info, $data);
            }
            $this->view->assign("show", $family);
            render("family/edit");
        } else {
            render("error", "家庭未找到");
        }
    }

    function del($id) {
        $family = $this->family->get_family($id);
        if ($family) {
            if ($family['user_id'] == $this->auth->user_id) {
                $count = $this->db->one("SELECT COUNT(*) AS num FROM {pre}user WHERE family_id='{$id}' AND id!='{$this->auth->user_id}'");
                if ($count['num'] == 0) {
                    user_basic(array('family_id' => 0));
                    $this->db->query("DELETE FROM {pre}family WHERE id = '{$id}' AND user_id='{$this->auth->user_id}'");
                    $code = 0;
                    $content = '删除家庭组成功!';
                } else {
                    $code = -3;
                    $content = '必须先删除成员后才能删除家庭组!';
                }
            } else {
                $code = -2;
                $content = "无法进行越权操作!";
            }
        } else {
            $code = -1;
            $content = '家庭组未找到!';
        }
        json_output($code, $content);
    }

    function join($id) {
        $family = $this->db->one("SELECT * FROM {pre}family WHERE uuid='{$id}'");
        if ($family) {
            if ($this->auth->fields['family_id'] == 0) {
                user_basic(array('family_id' => $family['id']));
                $this->db->query("UPDATE {pre}family SET members=members+1 WHERE id='{$family['id']}'");
                $code = 0;
                $content = '操作成功';
            } else {
                $code = -2;
                $content = '请勿重复加入';
            }
        } else {
            $code = -1;
            $content = '家庭组未找到';
        }
        json_output($code, $content);
    }

    function quit($id) {
        $family = $this->family->get_family($id);
        if ($family) {
            if ($this->auth->fields['family_id'] == $id) {
                user_basic(array('family_id' => 0));
                $this->db->query("UPDATE {pre}family SET members=members-1 WHERE id='{$this->auth->fields['family_id']}'");
                $code = 0;
                $content = '操作成功';
            } else {
                $code = -2;
                $content = '参数错误';
            }
        } else {
            $code = -1;
            $content = '家庭组未找到';
        }
        json_output($code, $content);
    }

    function remove($id) {
        $user = $this->auth->get_user_data($id);
        if ($user) {
            if ($user['family_id'] == $this->auth->fields['family_id']) {
                user_basic(array('family_id' => 0), $id);
                $this->db->query("UPDATE {pre}family SET members=members-1 WHERE id='{$this->auth->fields['family_id']}'");
                $code = 0;
                $content = '操作成功!';
            } else {
                $code = -2;
                $content = '无法越权操作!';
            }
        } else {
            $code = -1;
            $content = '成员未找到!';
        }
        json_output($code, $content);
    }

}